import datetime
from haystack import indexes
from haystack import site
from projects.models import Project, Release


class ProjectIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)
    creator = indexes.CharField(model_attr='creator')
    created = indexes.DateTimeField(model_attr='created')
    title = indexes.CharField(model_attr='name')
    url = indexes.CharField(indexed=False, use_template=True)

    def get_queryset(self):
        "Used when the entire index for model is updated."
        return Project.objects.filter(is_inactive=False, hide=False)


site.register(Project, ProjectIndex)


class ReleaseIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)
    creator = indexes.CharField(model_attr='creator')
    created = indexes.DateTimeField(model_attr='created')
    title = indexes.CharField(model_attr='version')
    url = indexes.CharField(indexed=False, use_template=True)

    def get_queryset(self):
        "Used when the entire index for model is updated."
        return Release.objects.filter(project__is_inactive=False,
                                      project__hide=False)
                                      
    def prepare_title(self, object):
        return u"%s %s" % (object.project, object.version)


site.register(Release, ReleaseIndex)